package com.thp.bigdata.thread.lock;

import java.util.ArrayList;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MyLockTest {
	
	
	private static ArrayList<Integer> arrayList = new ArrayList<Integer>();
	
	static Lock lock = new ReentrantLock();
	
	
	
	public static void main(String[] args) {
		new Thread() {
			public void run() {
				Thread thread = Thread.currentThread();
				lock.lock();
				
				System.out.println(thread.getName() + "得到了锁");
				for(int i = 0; i < 5; i++) {
					arrayList.add(i);
				}
				System.out.println(thread.getName() + "释放了锁");
				lock.unlock();
			};
		}.start();
		
		
		new Thread() {
			public void run() {
				Thread thread = Thread.currentThread();
				lock.lock();
				System.out.println(thread.getName() + "得到了锁");
				for(int i = 0; i < 5; i++) {
					arrayList.add(i);
				}
				System.out.println(thread.getName() + "释放了锁");
				lock.unlock();
			};
		}.start();
		
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(arrayList);
		
	}
}
